// pages/choose/choose.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uhide: 1,
    nextBtn: true,
    _num: 1,
    leftNav: [{
        "name": "你懂我吗",
        "id": 1
      },
      {
        "name": "电影口味",
        "id": 2
      },
      {
        "name": "择偶标准",
        "id": 3
      },
      {
        "name": "爱情观",
        "id": 4
      }
    ],
    dataInfo: [{
        "id": 1,
        "title": "汽车维修类课程",
        "option": [{
            "opt": "A",
            "ion": "喜欢",
            "checked": "1"
          },
          {
            "opt": "B",
            "ion": "不喜欢",
            "checked": "1"
          }
        ]
      },
      {
        "id": 2,
        "title": "电气自动化",
        "option": [{
            "opt": "A",
            "ion": "哈哈",
            "checked": "1"
          },
          {
            "opt": "B",
            "ion": "呵呵",
            "checked": "1"
          }
        ]
      },
      {
        "id": 3,
        "title": "你好",
        "option": [{
            "opt": "A",
            "ion": "嘿嘿",
            "checked": "1"
          },
          {
            "opt": "B",
            "ion": "呵呵啊啊啊多",
            "checked": "1"
          }
        ]
      }
    ],
    //定义选项答案数组
    jobList: [],
    sum: 1,
    count: 0,
    account: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.num);
    this.setData({
      account: options.num
    })


    var answer = wx.getStorageSync('answer');
    console.log(answer)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  navClick: function (event) {
    console.log(event.currentTarget.dataset.id)
    this.setData({
      _num: event.currentTarget.dataset.id
    })
  },
  //选题
  toggleBtn: function (res) {
    var uhide = this.data.uhide;
    var that = this;
    var dataInfo = this.data.dataInfo; //获取Json数组
    var sum = that.data.sum;
    var opt = res.currentTarget.dataset.opt //选项
    var id = res.currentTarget.dataset.id //题id
    var ind = res.currentTarget.dataset.ind // 类名
    var index = res.currentTarget.dataset.index //题id
    var _list = [{
      'id': id,
      'opt': opt
    }]

    // console.log(dataInfo[index].option[ind])
    for (let i = 0; i < dataInfo[index].option.length; i++) {
      dataInfo[index].option[i].checked = 1
    }
    dataInfo[index].option[ind].checked = 2
    // var jobList = that.data.jobList.concat(_list); //加入数组
    var jobList = _list.concat(that.data.jobList); //加入数组


    that.setData({
      jobList: jobList,
      dataInfo: dataInfo
    })


    function unique(arr) {
      // const res = new Map();
      // return arr.filter((a) => !res.has(a.id) && res.set(a.id, 1))
      var newArr = [];
      for (var i = 0; i < arr.length; i++) {
        var isHas = false
        for (let k = 0; k < newArr.length; k++) {
          if (arr[i].id == newArr[k].id) {
            // console.log(arr[i])
            isHas = true;
            break;
          }
        }
        if (!isHas) {
          newArr.push(arr[i])
        }
      }
      return newArr;
    }

    var answer = unique(jobList);
    that.setData({
      count: answer.length
    })
    console.log(answer);
    wx.setStorageSync('answer', answer);
  },
  delClick: function (event) {
    var id = event.currentTarget.dataset.id;
    console.log(id)

    var answer = wx.getStorageSync('answer');
    console.log(answer)
    answer = answer.filter(item => item.id !== id);
    console.log(answer)
    wx.setStorageSync('answer', answer);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})